import React from 'react'
import Styles from '../../assets/styles/login.module.less'
import { LockOutlined, UserOutlined } from '@ant-design/icons';
import { Button, Checkbox, Form, Input, message } from 'antd';
import { Login } from '../../apis/entrance';
import { useNavigate } from 'react-router';
import { getMenu } from '../../apis/menu';

type Props = {
    getIsCom: any
}

const ComLogin = (props: Props) => {
    const navigate = useNavigate()
    const onFinish = async (values: any) => {
        console.log('Success:', values);
        const res = await Login(values)
        // console.log(res);
        if (res.status === 200) {
            localStorage.setItem('token', res.data.data.token)
            localStorage.setItem('config', JSON.stringify(res.data.data.config))
            localStorage.setItem('user', JSON.stringify(res.data.data.user))
            localStorage.setItem('roles', res.data.data.roles)
            localStorage.setItem('exipreTime', res.data.data.exipreTime)
            localStorage.setItem('permissions', res.data.data.permissions)
            message.success(res.data.message)

            // console.log('res.data.data.user.username',res.data.data.user.username);

            const item = await getMenu(res.data.data.user.username)
            if (item.status === 200) {
                localStorage.setItem('menu', JSON.stringify(item.data))
            }

            // console.log('item',item);


            navigate('/home')
        } else {
            message.success(res.data.message)
        }

    };

    const onFinishFailed = (errorInfo: any) => {
        console.log('Failed:', errorInfo);
    };

    const SettleBtn = () => {
        props.getIsCom(false)
    }
    return (
        <div className={Styles.cont}>
            <Form
                name="basic"
                initialValues={{ remember: true }}
                onFinish={onFinish}
                onFinishFailed={onFinishFailed}
                autoComplete="off"
                style={{ width: '400px', margin: '0px auto' }}
            >
                <Form.Item
                    name="username"
                    rules={[{ required: true, message: 'Please input your username!' }]}
                >
                    <Input prefix={<UserOutlined className="site-form-item-icon" />} />
                </Form.Item>

                <Form.Item
                    name="password"
                    rules={[{ required: true, message: 'Please input your password!' }]}
                >
                    <Input.Password prefix={<LockOutlined className="site-form-item-icon" />} />
                </Form.Item>
                <Form.Item>
                    <Button type="primary" htmlType="submit" style={{ width: '400px' }}>
                        登录
                    </Button>
                </Form.Item>
            </Form>
            <div className={Styles.foot}>
                <a href="#" onClick={SettleBtn}>商家入驻</a>
            </div>
        </div>
    )
}

export default ComLogin